package com.sxfech.project.auth.controller;

import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.endpoint.CheckTokenEndpoint;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

/**
 * Created by Administrator on 2017/11/7 0007.
 */
@Controller
public class CheckTokenController {


    @Autowired
    CheckTokenEndpoint endpoint;

//    @Autowired
//    private DatabaseUserDetailService service;

    @RequestMapping(value = "/oauth/check_token")
    @ResponseBody
    public Map<String, ?> user(HttpServletRequest request){
        int len = OAuth2AccessToken.BEARER_TYPE.length() + 1;
        String token_ = request.getHeader("authorization");
        if(token_.length() > len){
            token_ = token_.substring(len);
        }
        Map result = endpoint.checkToken(token_);
        if(result.containsKey("user_name")){
            String userName = MapUtils.getString(result,"user_name");
//            result.put("user",service.loadUser(userName));
            System.out.println(userName);
        }
        return result;
    }

}
